/*
 *  CGImage-SMExtras.c
 *  Seams
 *
 *  Created by Charles Osmer on 3/26/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */

#include "CGImage-SMExtras.h"

unsigned int *CGImageGetRGBData(const CGImageRef imageRef)
{
    size_t width = CGImageGetWidth(imageRef);
    size_t height = CGImageGetHeight(imageRef);

    unsigned int *rgbData = malloc(width*height*sizeof(unsigned int));

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(rgbData, width, height, 8, width*4, colorSpace, kCGBitmapByteOrder32Little|kCGImageAlphaNoneSkipLast);
    
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
    CGContextSetShouldAntialias(context, false);
    
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
    
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    
    return rgbData;
}